/* << fastinsert report library >> Debug.cpp Oct 31, 2010
 *
 * 	http://code.google.com/p/libfir
 *
 * Copyright (c) 2005 Pavlo Drobov (drobov.pavel@gmail.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */


#include <fstream>
#include <time.h>

#include "Debug.h"

#ifdef _WIN32

#include "windows.h"
void raiseMessageBox(const char* txt)
{
	char buf[0xFF];
	::sprintf(buf,"%s",txt);
	MessageBox(NULL,buf,"reportLIB",MB_OK);
}
#endif //_WIN32


Debug * Debug::Instance = NULL;

Debug::Debug(std::ostream *param) :
	out(param)
{
	*out << "===================init debug ===============================" << std::endl;
}

Debug* Debug::getInstanceRef(std::ostream* out)
{
	if(!Instance)
		Instance = new Debug(out);

	return Instance;
}

Debug& Debug::getInstance(std::ostream*  out)
{
	if(!Instance)
		Instance = new Debug(out);

	return *Instance;
}

Debug& Debug::getInstance(const char* filePath)
{
	std::ofstream *out = new std::ofstream(filePath, std::ios::trunc);
	if(Instance)
		delete Instance;

	Instance = new Debug(out);
	return  *Instance;
}


std::string Debug::parseSvnIdKeyword(const char* c)
{
	std::string str(c);
	try
	{
		int index_start = str.find(':',1) +1;
		int index_end = str.find('$',1) -1;
		str = str.substr(index_start, index_end - index_start );
	}
	catch(...)
	{
	}

	return str;
}



//YYMMDD-HHMMSS
//atomic demand
std::string Debug::getCurrentTime()
{
	char buf[0xFF];
	time_t ltime;
	::time(&ltime);
	::strftime(buf, sizeof(buf), "%y%m%d-%H%M%S", ::localtime(&ltime));
	return std::string(buf);
}

std::string Debug::getCurrentFormatTime()
{
	char buf[0xFF];
	time_t ltime;
	::time(&ltime);
	::strftime(buf, sizeof(buf), "%d %b %G  %X ", ::localtime(&ltime));
	return std::string(buf);
}

Debug& Debug::operator <<(const int i)
{
	*out << std::dec << i;
	return *this;
}

Debug& Debug::operator <<(const float i)
{
//TODO: check output float std::dec
	*out << std::dec << i;
	return *this;
}


Debug& Debug::operator <<(const bool b)
{
	*out << (b ? "Yes[bool]" : "No[bool]");
	return *this;
}


Debug& Debug::operator <<(const __u32 i)
{
	char buf[32];
	::sprintf(buf,"%u",i);
	*out << std::dec << i;
	return *this;
}

Debug& Debug::operator <<(const __u64 i)
{
	char buf[32];
	::sprintf(buf,"%lu",i);
	*out << std::dec << buf;
	return *this;
}

Debug& Debug::operator <<(const __bigint i)
{
	char buf[32];
	::sprintf(buf,"%li",i);
	*out <<  std::dec << buf;
	return *this;
}


Debug& Debug::operator <<(const char *msg)
{
	if(msg)
	{
		*out << msg;
		if(*msg == '\n' || *msg == '\r')
		{
			*out << std::flush;
		}
	}
	else
		*out << "NULL";

	return *this;
}

Debug& Debug::operator <<(const std::string& msg)
{
	*out << msg;
	return *this;
}

std::string Debug::ip2str(unsigned int ip)
{
	char buf[15];
	const unsigned char * rabbit = (unsigned char*)&ip;
	sprintf(buf,
			"%u.%u.%u.%u",
			rabbit[0],
			rabbit[1],
			rabbit[2],
			rabbit[3]);
	return std::string(buf);
}

void Debug::endl()
{
	*out << std::endl;
}

Debug::~Debug()
{
	DEBUG_MSG("Destructor DEBUG");
	std::ofstream * file = dynamic_cast<std::ofstream*> (out);
	if(file)
	{
		file->close();
		delete file;
	}
	endl();
}




